package com.fiveflavors.suittar.gui;

import java.lang.reflect.Method;
import java.net.URL;

/////////////////////////////////////////////////////////
//This source code was referred to :
//Bare Bones Browser Launch                            //
//Version 1.5 (December 10, 2005)                      //
//By Dem Pilafian                                      //
/////////////////////////////////////////////////////////

public class BrowserLauncher {

	public static boolean openURL(URL url) {
		return openURL(url.toString());
	}
	
	public static boolean openURL(String url) {
		
		String osName = System.getProperty("os.name");
		
		try {
			
			//Mac OS
			if (osName.startsWith("Mac OS")) {
				Class<?> fileMgr = Class.forName("com.apple.eio.FileManager");
				Method openURL = fileMgr.getDeclaredMethod("openURL",new Class[] {String.class});
				openURL.invoke(null, new Object[] {url});
				return true;
			}
			
			//Windows
			else if (osName.startsWith("Windows")) {
				Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
				return true;
			}
			
			//assume Unix or Linux
			else {
				
				String[] browsers = {
						"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
				String browser = null;
				for (int count = 0; count < browsers.length && browser == null; count++)
					if (Runtime.getRuntime().exec(
							new String[] {"which", browsers[count]}).waitFor() == 0)
						browser = browsers[count];
				if (browser == null) {
					return false;
				}
				else {
					Runtime.getRuntime().exec(new String[] {browser, url});
					return true;
				}
			}
		}
		catch (Exception e) {
			return false;
		}
	}

}
